package bg.unisofia.fmi.tis.ui.bean;

import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.model.SelectItem;

import bg.unisofia.fmi.tis.model.Comment;
import bg.unisofia.fmi.tis.model.Thesis;
import bg.unisofia.fmi.tis.model.ThesisStatus;
import bg.unisofia.fmi.tis.model.User;
import bg.unisofia.fmi.tis.service.JBPMUtil;
import bg.unisofia.fmi.tis.service.ModelService;

public class ThesisBean extends BackingBeanBase {
	
	private Thesis thesis;
	private User user;
	
	private String supervisor;
	private String accepted = "A";
	private String comment;
	
	private String reviewer;
	
	public void update(Thesis thesis, User user) {
		this.thesis = thesis;
		this.user = user;
	}
	
	public String actionSubmitProposal() {
		
		if (thesis.getTasks().length() > 500) {
			addMessage(FacesMessage.SEVERITY_ERROR, "proposalForm", "Tasks field cannot exceed 500 characters.");
			return "";
		}
		
		if (thesis.getExecutionTime() > 70) {
			addMessage(FacesMessage.SEVERITY_ERROR, "proposalForm", "Execution time cannot exceed 70.");
			return "";
		}
		
		if (supervisor == null) {
			addMessage(FacesMessage.SEVERITY_ERROR, "proposalForm", "Fill in supervisor name.");
			return "";
		}
		
		User teacher = ModelService.findByName(User.class, supervisor);
		if (teacher == null) {
			addMessage(FacesMessage.SEVERITY_ERROR, "proposalForm", "There is no supervisor '" + supervisor + "' in the system.");
			return "";
		}
		
		thesis.setStudent(user);
		thesis.setSupervisor(teacher);
		thesis.setStatus(ThesisStatus.PENDING);
		thesis.setProposedOn(new Date());
		
		thesis = (Thesis) ModelService.save(thesis);
		
		JBPMUtil.startProcess(thesis);
		
		getManagedBean(UserBean.class).update(user);
		
		return "studentDashboard";
	}

	public String actionSubmitThesis() {
		
		thesis.setStatus(ThesisStatus.PROPOSED);
		thesis = (Thesis) ModelService.save(thesis);
		
		JBPMUtil.continueProcess(thesis);
		
		getManagedBean(UserBean.class).update(user);
		
		return "studentDashboard";
	}
	
	public String actionReviewProposal() {

		if (reviewer == null) {
			addMessage(FacesMessage.SEVERITY_ERROR, "proposalForm", "Fill in reviewer name.");
			return "";
		}
		
		User teacher = ModelService.findByName(User.class, reviewer);
		if (teacher == null) {
			addMessage(FacesMessage.SEVERITY_ERROR, "proposalForm", "There is no reviewer '" + reviewer + "' in the system.");
			return "";
		}
		
		if (comment != null && comment.length() > 0) {
			Comment tcomm = new Comment();
			tcomm.setContent(comment);
			tcomm.setProfessor(user);
			tcomm.setThesis(thesis);
			tcomm.setSubmittedOn(new Date());
			tcomm = (Comment) ModelService.save(tcomm);
			
			thesis.getComments().add(tcomm);
		}
		
		if (getCanShowAcceptReject()) {
			ThesisStatus status = ThesisStatus.ACCEPTED;
			if (accepted.equals("R")) {
				status = ThesisStatus.REJECTED;
			}
			
			thesis.setStatus(status);
		}
		
		thesis.setReviewer(teacher);
		thesis = (Thesis) ModelService.save(thesis);
		
		JBPMUtil.continueProcess(thesis);
		
		getManagedBean(UserBean.class).update(user);
		
		return "teacherDashboard";
	}
	
	public String actionReview() {
		thesis.setStatus(ThesisStatus.REVIEWED);		
		thesis = (Thesis) ModelService.save(thesis);
		
		JBPMUtil.continueProcess(thesis);
		
		getManagedBean(UserBean.class).update(user);
		
		return "teacherDashboard";
	}
	
	public SelectItem[] getAcceptItems() {
		SelectItem[] items = new SelectItem[2];
		
		items[0] = new SelectItem("A", "Accept");
		items[1] = new SelectItem("R", "Reject");
		
		return items;
	}
	
	public boolean getCanShowAcceptReject() {
		return thesis.getSupervisor().equals(user);
	}
	
	public Thesis getThesis() {
		return thesis;
	}

	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getSupervisor() {
		return supervisor;
	}

	public void setSupervisor(String supervisor) {
		this.supervisor = supervisor;
	}

	public String getAccepted() {
		return accepted;
	}

	public void setAccepted(String accepted) {
		this.accepted = accepted;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getReviewer() {
		return reviewer;
	}

	public void setReviewer(String reviewer) {
		this.reviewer = reviewer;
	}
}
